﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYOpeningCommon.DataEnum;
using HYOpeningCommon.DbOpening;
using HYOpeningCommon.OpeningProfile;
using HYOpeningCommon.Public;

namespace HYOpeningDesignInput.OpeningCheck
{
	public partial class OpeningCheckReportForm : ConfigDataBaseForm
	{
		public OpeningCheckReportForm(UIDocument uiDoc, LinkedList<DbOpeningBase> lstOpening, WindowHandle hWndRevit)
		{
			this.InitializeComponent();
			base.SetFormLocation(this.strXmlPath);
			this.m_uiDoc = uiDoc;
			this.m_hWndRevit = hWndRevit;
			this.m_lstOpening = lstOpening;
			this.dgOpening.RowHeadersVisible = false;
			this.dgOpening.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			WinDPIManager.GetWinFormXYDpi(ref this.m_dXScale, ref this.m_dYScale);
			this.m_iInitExWidth = Convert.ToInt32(900.0 * this.m_dXScale);
			this.m_iInitUnExWidth = Convert.ToInt32(340.0 * this.m_dXScale);
			this.m_iHeight = Convert.ToInt32(475.0 * this.m_dYScale);
			base.Width = this.m_iInitUnExWidth;
			base.Height = this.m_iHeight;
			this.SetDataGridViewWidth();
			this.SetOpeningData2DataGridView();
		}

		private void SetDataGridViewWidth()
		{
			bool visible = true;
			double num = (double)this.dgPlane.Width;
			if (this.btnExpansion.Text == "-->")
			{
				visible = false;
				double num2 = num / 8.0;
				if (this.dgOpening.ColumnCount > 5)
				{
					this.dgOpening.Columns[0].Width = Convert.ToInt32(num2 * 0.8);
					this.dgOpening.Columns[1].Width = Convert.ToInt32(num2 * 0.8);
					this.dgOpening.Columns[2].Width = Convert.ToInt32(num2 * 0.8);
					this.dgOpening.Columns[3].Width = Convert.ToInt32(num2 * 0.8);
					this.dgOpening.Columns[4].Width = Convert.ToInt32(num2 * 1.4);
					this.dgOpening.Columns[8].Width = Convert.ToInt32(num2 * 3.6);
				}
			}
			else
			{
				double num3 = num / 20.0;
				if (this.dgOpening.ColumnCount > 10)
				{
					this.dgOpening.Columns[0].Width = Convert.ToInt32(num3 * 0.8);
					this.dgOpening.Columns[1].Width = Convert.ToInt32(num3 * 0.8);
					this.dgOpening.Columns[2].Width = Convert.ToInt32(num3 * 0.8);
					this.dgOpening.Columns[3].Width = Convert.ToInt32(num3 * 0.8);
					this.dgOpening.Columns[4].Width = Convert.ToInt32(num3 * 1.4);
					this.dgOpening.Columns[5].Width = Convert.ToInt32(num3 * 1.4);
					this.dgOpening.Columns[6].Width = Convert.ToInt32(num3 * 1.4);
					this.dgOpening.Columns[7].Width = Convert.ToInt32(num3 * 3.0);
					this.dgOpening.Columns[8].Width = Convert.ToInt32(num3 * 3.0);
					this.dgOpening.Columns[9].Width = Convert.ToInt32(num3 * 3.0);
					this.dgOpening.Columns[10].Width = Convert.ToInt32(num3 * 4.0);
				}
			}
			if (this.dgOpening.ColumnCount > 10)
			{
				this.dgOpening.Columns[5].Visible = visible;
				this.dgOpening.Columns[6].Visible = visible;
				this.dgOpening.Columns[7].Visible = visible;
				this.dgOpening.Columns[9].Visible = visible;
				this.dgOpening.Columns[10].Visible = visible;
			}
		}

		private void SetOpeningData2DataGridView()
		{
			this.dgOpening.AllowUserToAddRows = true;
			foreach (DbOpeningBase dbOpeningBase in this.m_lstOpening)
			{
				string strAxisX = dbOpeningBase.m_strAxisX;
				string strAxisY = dbOpeningBase.m_strAxisY;
				string strFloorPlan = dbOpeningBase.m_strFloorPlan;
				int num = (int)dbOpeningBase.m_eHostObject;
				string text = StrHostObj.m_strHostObj[num];
				string text2 = string.Empty;
				num = (int)dbOpeningBase.m_eCutObject;
				if (dbOpeningBase is DbCompositeOpening)
				{
					text2 = "综";
				}
				else
				{
					text2 = StrCutObj.m_strCutObj[num];
				}
				string text3 = string.Empty;
				if ((dbOpeningBase.m_eCutObject == ECutObject.eHvPipe || dbOpeningBase.m_eCutObject == ECutObject.eWaterPipe || dbOpeningBase.m_eCutObject == ECutObject.ePipeMulti) && dbOpeningBase is DbRectWallOpening)
				{
					text3 = AssistFunc.feetToMM((dbOpeningBase.m_profile as OpeningProfileRect).m_cutHeight).ToString("f0");
				}
				else
				{
					text3 = dbOpeningBase.GetCutSize(false);
				}
				string openingSize = dbOpeningBase.GetOpeningSize(false);
				string strDesignDate = dbOpeningBase.m_strDesignDate;
				string strRemark = dbOpeningBase.m_strRemark;
				string text4 = "未开";
				if (dbOpeningBase.m_eOpeningState == EOpeningState.eOpen)
				{
					text4 = "已开";
				}
				this.dgOpening.Rows.Add(new object[]
				{
					text4,
					this.dgOpening.RowCount,
					strAxisX,
					strAxisY,
					text,
					text2,
					strFloorPlan,
					text3,
					openingSize,
					strDesignDate,
					strRemark
				});
				this.dgOpening.Rows[this.dgOpening.RowCount - 2].Tag = dbOpeningBase;
			}
			this.dgOpening.AllowUserToAddRows = false;
		}

		private void btnExpansion_Click(object sender, EventArgs e)
		{
			if (this.btnExpansion.Text == "-->")
			{
				this.btnExpansion.Text = "<--";
				base.Width = this.m_iInitExWidth;
				return;
			}
			this.btnExpansion.Text = "-->";
			base.Width = this.m_iInitUnExWidth;
			base.Height = this.m_iHeight;
		}

		private void dgOpening_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex == -1)
			{
				return;
			}
			CommonInterface.DoubleClick2Location(this.dgOpening.Rows[e.RowIndex].Tag as DbOpeningBase, this.m_uiDoc);
		}

		private void btnLookUp_Click(object sender, EventArgs e)
		{
			if (this.dgOpening.SelectedRows.Count > 0)
			{
				CommonInterface.DoubleClick2Location(this.dgOpening.Rows[this.dgOpening.SelectedRows[0].Index].Tag as DbOpeningBase, this.m_uiDoc);
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void OpeningCheckReport_Resize(object sender, EventArgs e)
		{
			this.SetDataGridViewWidth();
		}

		[DllImport("user32.dll")]
		private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendESC()
		{
			List<Element> selElements = new List<Element>();
			RevitVersionFuncs.SetSelection(this.m_uiDoc.Selection, selElements);
			this.m_uiDoc.RefreshActiveView();
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			OpeningCheckReportForm.SendMessage(this.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			OpeningCheckReportForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			OpeningCheckReportForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void OpeningCheckReportForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			XmlDocument xmlDocument = new XmlDocument();
			if (File.Exists(this.strXmlPath))
			{
				XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
				xmlReaderSettings.IgnoreComments = true;
				XmlReader xmlReader = XmlReader.Create(this.strXmlPath, xmlReaderSettings);
				xmlDocument.Load(xmlReader);
				XmlElement documentElement = xmlDocument.DocumentElement;
				documentElement.SetAttribute("LocationX", base.Location.X.ToString());
				documentElement.SetAttribute("LocationY", base.Location.Y.ToString());
				xmlReader.Close();
			}
			else
			{
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement("HistoryData");
				xmlDocument.AppendChild(xmlElement);
				xmlElement.SetAttribute("LocationX", base.Location.X.ToString());
				xmlElement.SetAttribute("LocationY", base.Location.Y.ToString());
			}
			xmlDocument.Save(this.strXmlPath);
			this.m_bFormClose = true;
			this.SendESC();
		}

		private void OpeningCheckReportForm_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Escape)
				{
					base.Close();
				}
			}
			catch
			{
			}
		}

		private int m_iInitExWidth;

		private int m_iInitUnExWidth;

		private int m_iHeight;

		private UIDocument m_uiDoc;

		private LinkedList<DbOpeningBase> m_lstOpening;

		private double m_dXScale = 1.0;

		private double m_dYScale = 1.0;

		private WindowHandle m_hWndRevit;

		public bool m_bFormClose;

		private string strXmlPath = Product.UserDataLocation + "\\OpeningCheckReportForm.xml";
	}
}
